//
//  ViewController.m
//  20-XML
//
//  Created by 李昊林 on 16/8/10.
//  Copyright © 2016年 李昊林. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "GDataXMLNode.h"
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, strong) NSMutableArray *stuArray;
@property (nonatomic, strong) NSMutableArray *domStuArray;
@property (nonatomic, strong) NSString *currentElementName;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
}
// XML 分为两种解析方式
// 第一种 SAX 逐行解析 -- 使用系统的解析器 NSXMLParser
- (IBAction)saxParse:(id)sender {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:path];
    NSXMLParser *xmlPaser = [[NSXMLParser alloc] initWithData:xmlData];
    xmlPaser.delegate = self;
    [xmlPaser parse];
    
}
// 1.解析器开始
- (void)parserDidStartDocument:(NSXMLParser *)parser {
    self.stuArray = [NSMutableArray array];
}
// 2.遇到结点头 方法的回调, 记录当前节点，方便后续赋值
//  打印所有节点头
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
    NSLog(@"结点头%@", elementName);
    _currentElementName = elementName;
    if ([elementName isEqualToString:@"student"]) {
        Student *student = [Student new];
        [_stuArray addObject:student];
    }
}
// 3.节点的值 根据节点决定值。 没有值时对应的结果
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    //      string 是 \n \t
    //    if (![string hasSuffix:@" "] && ![string hasSuffix:@"\n"]) {
    if (_currentElementName) {
        Student *stu = [_stuArray lastObject];
        [stu setValue:string forKey:_currentElementName];
        NSLog(@"节点的值%@ --- 节点值结束", string);
    }
    
    
    //    }
    
}


//4. 遇到节点尾 方法的回调 判断当前节点结束
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    NSLog(@"结点尾/%@", elementName);
    _currentElementName = nil;
}

// 5.解析器结束
- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"%@",_stuArray);
}

// 解析失败时，调用
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    
}

// 第二中解析方式， DOM解析
// 关于GDataXMLNode的引入
// 1.引入.h .m 文件
// 2. 引入系统动态libxml2.tbd
// 3.根据GDataXMLNode头文件中的提示 ，添加/usr/include/libxml2
// 4. 如果是ARC环境， 需要对该文件进行MRC编译， 添加-fno-objc-arc
- (IBAction)domParse:(id)sender {
    
    self.domStuArray = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    //   1. 创建GDataXMLDocument对象，
    //    2.根据xml的Data数据生成一个树形结构的 Document
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
    //    根据document获取根节点
    GDataXMLElement *rootElement = document.rootElement;
    NSLog(@"%@", rootElement.name);
    //    根据节点获取 子节点数组
    NSArray *rootChhildElement = rootElement.children;
    for (GDataXMLElement *childElement in rootChhildElement) {
        //        NSLog(@"childElement name : %@", childElement.name);
        //遍历子节点数组
        Student *stu = [Student new];
        //对应所有student节点
        [self.domStuArray addObject:stu];
        //遍历所有节点的子节点
        for (GDataXMLElement *grandSonElement in childElement.children) {
            //            NSLog(@"grandSonElement :%@", grandSonElement.name);
            //赋值
            [stu setValue:grandSonElement.stringValue forKey:grandSonElement.name];
        }
    }
    NSLog(@"%@",self.domStuArray);
}
//XMLReader
//将XML结构没转换成Dictionary
//


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
